# ~*~ coding: utf-8 ~*~


class WSProxy(object):
    """代理websocket发送数据"""
    connections = {}

    def __init__(self, ws, room):
        self.ws = ws
        self.room = room

    @classmethod
    def new_wsproxy(cls, ws, room):
        wsporxy = cls(ws, room)
        cls.connections[wsporxy.room] = wsporxy
        return wsporxy

    @classmethod
    def get_wsproxy(cls, room):
        return cls.connections.get(room)

    @classmethod
    def remove_wsproxy(cls, room):
        cls.connections.pop(room, None)

    def send(self, type, data):
        """
        服务器端发送数据到客户端
        :param data:
        :return:
        """
        self.ws.emit(type, data, room=self.room)
